package edu.rutgers.moko;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;

import edu.rutgers.moko.data.util.FieldAccessUtil;
import edu.rutgers.moko.data.util.OMField;
import edu.rutgers.moko.data.util.SecurityUtil;

/**
 * 
 * @author Darja Krushevskaja
 *
 */
public class StateServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	//private final File dir = new File("/home/dashali/Documents/school/projects/server");
	private final File dir = new File("/home/root/server");
	private final File msgs = new File(dir, "properties.txt");

	@SuppressWarnings("unchecked")
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		PrintWriter out = res.getWriter();
		String tmp;
		
		                                                                                                                                                                                                                                                                                                                                                                                                                             List<String> lines = new ArrayList<String>();
		if (msgs.exists()) {
			lines = FileUtils.readLines(msgs);
		}
		FieldAccessUtil faUtil = new FieldAccessUtil(lines);
		
		
		out.println("<html>");
		out.println("<head><title>OM >> Static Information Page</title>"+ServletUtil.css()+"</head>");
		
		out.println("<body>");
		out.println("<h1>OM >> General Information Page</h1>");
		out.println("<a href=\"/state\">Home</a>");
		out.println(" | <a href=\"/rss\">RSS</a>");
		out.println(" | <a href=\"/msg\">Messages</a>");
		if(SecurityUtil.localAccess(req)){
			out.print(" | <a href=\"/scs\">Adjust sequrity controls</a>");
		}
		out.println("<br/><br/>");
		
		File file;
		Boolean isEmpty = true;
		if(faUtil.isVisible(req.getRemoteAddr(), OMField.VERSION)){
			isEmpty = false;
			file = new File("/etc/om-version");
	        lines = FileUtils.readLines(file);
	        for (String string : lines) {
				String[] s = string.split(":");
				if(s.length == 2 ){
				out.println("<b>"+s[0]+"</b>" + " " + s[1]+ "<br/>");
				}
			}
		}
		
		if(faUtil.isVisible(req.getRemoteAddr(), OMField.BATTERY_TECHNOLOGY)){
			isEmpty = false;
	        file  = new File("/sys/devices/platform/bq27000-battery.0/power_supply/bat/technology");
	        lines = FileUtils.readLines(file);
	        out.println("<b>Battery technology is</b> " + lines + "<br/>");
		}
		
		if(faUtil.isVisible(req.getRemoteAddr(), OMField.BATTERY_CAPACITY)){
			isEmpty = false;
			file = new File("/sys/devices/platform/bq27000-battery.0/power_supply/bat/capacity");
			lines = FileUtils.readLines(file);
			out.println("<b>Battery capacity is</b> " + lines + "<br/>");
		}
		
		if(faUtil.isVisible(req.getRemoteAddr(), OMField.BATTERY_TTE)){
			isEmpty = false;
			file = new File("/sys/devices/platform/bq27000-battery.0/power_supply/bat/time_to_empty_now");
			lines = FileUtils.readLines(file);
			if(lines != null && lines.size()==1){
	        	tmp = secondsToString(Long.valueOf(lines.get(0)));
	        	out.println("<b>Battery time to empty</b> " + tmp + "<br/>");
			}
		}
		
		if(faUtil.isVisible(req.getRemoteAddr(), OMField.BRIGHTNESS_MAX)){
			isEmpty = false;
	        file  = new File("/sys/devices/platform/s3c2440-i2c/i2c-adapter/i2c-0/0-0073/backlight/pcf50633-bl/max_brightness");
	        lines = FileUtils.readLines(file);
	        out.println("<b>The maximum brightness of display is</b> " + lines+ "<br/>");
		}
		
		if(faUtil.isVisible(req.getRemoteAddr(), OMField.BRIGHTNESS_CURR)){
			isEmpty = false;
	        file  = new File("/sys/devices/platform/s3c2440-i2c/i2c-adapter/i2c-0/0-0073/backlight/pcf50633-bl/max_brightness");
	        lines = FileUtils.readLines(file);
	        if(lines != null && lines.size()==1){
	        	if(lines.get(0).equals("0") ){
	        		tmp = "off";
	        	} else {
	        		tmp = "on";
	        	}
	        	out.println("<b>Current bluetooth is</b> " + tmp+ "<br/>");
	        }
		}
        if(isEmpty){
        	out.println("Sorry, no data for you today! ;)");
        }
		out.print("<br/><br/><div id=\"footer\"><center><b>OM project for internet services</b> 2009<br>Darja Krushevskaja &amp;for more information: darja@cs</center></div>");
        out.println("</body>");
        out.println("</html>");
        
		out.close();
	}
	
	private String secondsToString(long time){
	    int seconds = (int)(time % 60);
	    int minutes = (int)((time/60) % 60);
	    int hours = (int)((time/3600) % 24);
	    String secondsStr = (seconds<10 ? "0" : "")+ seconds;
	    String minutesStr = (minutes<10 ? "0" : "")+ minutes;
	    String hoursStr = (hours<10 ? "0" : "")+ hours;
	    return new String(hoursStr + ":" + minutesStr + ":" + secondsStr);
	  }

}
